package com.gsi.webstore.platform.storefront.pipelet.orderprocess;

import com.intershop.beehive.core.capi.log.Logger;
import com.intershop.beehive.core.capi.naming.NamingMgr;
import com.intershop.beehive.core.capi.pipeline.Pipelet;
import com.intershop.beehive.core.capi.pipeline.PipelineDictionary;
import com.intershop.beehive.bts.capi.orderprocess.ProductLineItem;
import com.intershop.beehive.bts.capi.orderprocess.basket.Basket;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.intershop.beehive.core.capi.pipeline.PipeletExecutionException;
import com.intershop.beehive.core.internal.profile.BasicAddressPO;
import com.intershop.beehive.core.internal.profile.BasicAddressPOFactory;

/**
 * This pipelet returns a map with key as cart line item id and value as store
 * address id
 */
public class MapStoreAddressIDWithCartLineItems extends Pipelet
{

     /**
     * Constant used to access the pipeline dictionary with key 'StoreAddressIDMap'
     */
    public static final String DN_STORE_ADDRESS_IDMAP = "StoreAddressIDMap";
    /**
     * Constant used to access the pipeline dictionary with key 'Cart'
     */
    public static final String DN_CART = "Cart";
    String storeFrontIdAttribute = "STORE_ID";
    String storeCodeAttribute = "STORE_CODE";
    private BasicAddressPOFactory aBasicAddressPOFactory=null;
    public MapStoreAddressIDWithCartLineItems(){
        try{
            aBasicAddressPOFactory= (BasicAddressPOFactory)NamingMgr.getInstance().lookupFactory(BasicAddressPOFactory.FACTORY_NAME);
        }
        catch(Exception e){
           Logger.error(this, "Pipelet::MapStoreAddressIDWithCartLineItems", "Exception"+e.getMessage());
        }
    }
    /**
     * The pipelet's execute method is called whenever the pipelets gets
     * executed in the context of a pipeline and a request. The pipeline
     * dictionary valid for the currently executing thread is provided as a
     * parameter.
     * 
     * @param dict
     *            The pipeline dictionary to be used.
     * @throws PipeletExecutionException
     *             Thrown in case of severe errors that make the pipelet execute
     *             impossible (e.g. missing required input data).
     */
    public int execute(PipelineDictionary dict)
    throws PipeletExecutionException {        
        // lookup 'Cart' in pipeline dictionary
        Basket cart = (Basket)dict.get(DN_CART);
        if (null == cart)
        {
            throw new PipeletExecutionException("Mandatory input parameter 'Cart' not available in pipeline dictionary.");
        
        }

        Iterator<ProductLineItem> productLineItemsItr = cart.createProductLineItemsIterator();
        Iterator<BasicAddressPO> StoreAddressItr;
        HashMap<String,String> StoreAddressIDsMap = new HashMap<String,String> ();
        Long storeId = null;
        String StoreCode;
        String StoreId;
        BasicAddressPO aBasicAddress = null;
               
        if(productLineItemsItr!= null)
        {
            while(productLineItemsItr.hasNext())
            {
                ProductLineItem pli= productLineItemsItr.next();
                
                //get value of custom attribute storeid
                storeId=(Long)pli.getAttribute(storeFrontIdAttribute);
                
                //get value of custom attribute store code
                StoreCode=(String)pli.getAttribute(storeCodeAttribute);
                
                if(storeId != null && StoreCode != null)
                {
                    StoreId=storeId.toString();
                    String query="AddressNr='"+StoreCode+StoreId+"'";
                    StoreAddressItr=aBasicAddressPOFactory.getObjectsBySQLWhere(query).iterator();
                    if(StoreAddressItr != null && StoreAddressItr.hasNext()){
                        aBasicAddress= (BasicAddressPO)StoreAddressItr.next();
                    }
                    StoreAddressIDsMap.put(pli.getUUID(), aBasicAddress.getUUID()); 
               }
            }
        }
        // store 'StoreAddressIDMap' in pipeline dictionary
        // TODO Put the value in dictionary
        dict.put(DN_STORE_ADDRESS_IDMAP, StoreAddressIDsMap);
        return PIPELET_NEXT;
    }
}